//
//  RealNameCertifyVC.m
//  u76ho
//
//  Created by m-air-01 on 16/1/27.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "RealNameCertifyVC.h"

@interface RealNameCertifyVC ()

@end

@implementation RealNameCertifyVC
-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];

    if (_isWantCertify == YES) {
      //  AppDelegate * app = [UIApplication sharedApplication].delegate;
        //[app.viewController isHiddenCustomTabBarByBoolean:YES];
        self.navigationController.navigationBarHidden = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
   // [self addBackItem];
    self.navigationItem.title =  @"实名认证";
//    [self addNavgationBar];
    // Do any additional setup after loading the view.
}
-(void)main
{
    self.view.backgroundColor = [UIColor whiteColor];
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    [self.view addSubview:bgView];
    
    UIView * firstView = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(15), SIZETO6H(25), SCREEN_WIDTH-SIZETO6W(15)*2, 60)];
    firstView.backgroundColor = UIColorFromRGB(0xf6f6f6);
    [bgView addSubview:firstView];
    
//    UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(SIZETO6W(15), 10, firstView.width- SIZETO6W(15)*2, 40)];
//    textView.text = @"提示：为了让更多的企业能够精确的查找到您，请您填写真实有效身份ID。";
//    textView.textColor = UIColorFromRGB(0x94999f);
//    [firstView addSubview:textView];
    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(SIZETO6W(15), 10, firstView.width- SIZETO6W(15)*2, 40)];
    lab.text = @"提示：为了让更多的企业能够精确的查找到您，请您填写真实有效身份ID。";
    lab.numberOfLines = 0;
    lab.font = [UIFont systemFontOfSize:15];
    lab.textColor = UIColorFromRGB(0x94999f);
    lab.backgroundColor = UIColorFromRGB(0xf6f6f6);
    [firstView addSubview:lab];
    
    NSArray * placeHolderArr = @[@"姓名",@"身份证号"];
    for (int i = 0; i<2; i++) {
        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(SIZETO6W(20), firstView.bottom+SIZETO6H(25)+i*(20+SIZETO6H(50)), SCREEN_WIDTH-SIZETO6W(20)*2, 20)];
        textField.placeholder = placeHolderArr[i];
        [bgView addSubview:textField];
        textField.tag = i+10;
        UIView * lineV = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(20), textField.bottom+SIZETO6H(25), SCREEN_WIDTH-SIZETO6W(20)*2, 1)];
        lineV.backgroundColor = UIColorFromRGB(0xcccccc);
        [bgView addSubview:lineV];
        textField.delegate = self;
        
        if (i == 1) {
            textField.keyboardType = UIKeyboardTypeASCIICapable;

            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(SIZETO6W(15), textField.bottom+SIZETO6H(53), SCREEN_WIDTH-SIZETO6W(15)*2, 40);
            [btn setTitle:@"完成" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setBackgroundColor:UIColorFromRGB(0x6ec844)];
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            [bgView addSubview:btn];
        }
    }
}
-(void)realNameCeitifyWithName:(NSString *)name idcards:(NSString *)idcards
{
    __weak typeof(self)weakSelf = self;
    [U76HONet realNameCertificationWithAction:@"" code:[Singleton share].code realName:name idcard:idcards andDataBlock:^(NSDictionary *dic) {
        NSLog(@"真实姓名确认成功列 哈哈哈");
        if (_isWantCertify == YES) {
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"realNameNotiCase" object:nil];
            [weakSelf performSelector:@selector(back) withObject:nil afterDelay:1];
        }
        else
        {
            UITextField * textField2 = (UITextField *)[weakSelf.view viewWithTag:11];
            [self.delegate realNameDelegateWithIdcard:textField2.text];
            [weakSelf performSelector:@selector(back) withObject:nil afterDelay:1];
        }
    }];
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)btnAction:(UIButton *)btn
{
    UITextField * textField1 = (UITextField *)[self.view viewWithTag:10];
    [textField1 resignFirstResponder];
    
    UITextField * textField2 = (UITextField *)[self.view viewWithTag:11];
    [textField2 resignFirstResponder];
    
    if (![textField2.text isEqualToString:@""] && ![textField1.text isEqualToString:@""]) {
        [self realNameCeitifyWithName:textField1.text idcards:textField2.text];
    }

}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) {
        //        NSLog(@"开始登录了");
        
        UITextField * textField1 = (UITextField *)[self.view viewWithTag:10];
        [textField1 resignFirstResponder];
        
        UITextField * textField2 = (UITextField *)[self.view viewWithTag:11];
        [textField2 resignFirstResponder];
        
        if (![textField2.text isEqualToString:@""] && ![textField1.text isEqualToString:@""]) {
            [self realNameCeitifyWithName:textField1.text idcards:textField2.text];
        }
        return NO;
    }
    return YES;
    
    
    
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touch begin la");
    UITextField * textField1 = (UITextField *)[self.view viewWithTag:10];
    [textField1 resignFirstResponder];
    
    UITextField * textField2 = (UITextField *)[self.view viewWithTag:11];
    [textField2 resignFirstResponder];

}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
